package actions;
import java.text.*;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;
import beans.AbstractPagedQuery;
import service.ExpressShipPathService;
import beans.ExpressShipPathQuery;
import domain.ExpressShipPath;
/**
 * @author system auto
 */
public class ExpressShipPathMainAction extends AbstractPagedQueryAction {
	/**
	 * 用于查询快递线路.
	 */
	 private ExpressShipPathService expressShipPathService;
	/**
	  * 构造分页查询对象并进行分页查询.
	  * @param form action form
	  * @param request http request
	  * @param totalCount 记录总个数
	  * @param pagerOffset 查询的起始纪录位置
	  * @param itemsPerPage 每页多少条纪录
	  * @return AbstractPagedQuery Query
	  * @throws Exception any exception
	  */
	 @Override
	 public AbstractPagedQuery<ExpressShipPath> doPagedQuery(ActionForm form,
	       HttpServletRequest request, int totalCount, int pagerOffset,
	       int itemsPerPage) throws Exception {
	   DynaValidatorForm queryCondForm = (DynaValidatorForm) form;
	   ExpressShipPathQuery myQuery = new ExpressShipPathQuery();
	   // 设置查询的分页参数
	   myQuery.setItemsPerPage(itemsPerPage);
	   myQuery.setTotalCount(totalCount);
	   myQuery.setPagerOffset(pagerOffset);
	   	String startPos = queryCondForm.getString("qStartPos");
		if (null != startPos && !"".equals(startPos)) {
	   		myQuery.setStartPos(startPos);
	   	}
	   	String endPos = queryCondForm.getString("qEndPos");
		if (null != endPos && !"".equals(endPos)) {
	   		myQuery.setEndPos(endPos);
	   	}
	   	String serviceType = queryCondForm.getString("qServiceType");
		if (null != serviceType && !"".equals(serviceType)) {
	   		myQuery.setServiceType(serviceType);
	   	}
	   	String transPortType = queryCondForm.getString("qTransPortType");
		if (null != transPortType && !"".equals(transPortType)) {
	   		myQuery.setTransPortType(transPortType);
	   	}
   		// 调用ExpressShipPathService进行分页查询
        expressShipPathService.queryExpressShipPaths(myQuery);
        return myQuery;
  	}
  /**
   * 批量删除快递线路.
   * @param mapping mapping
   * @param form v
   * @param request
   * @param response
   * @return action forward
   * @throws Exception
   */
  public ActionForward batchDelete(ActionMapping mapping, ActionForm form,
          HttpServletRequest request, HttpServletResponse response)
          throws Exception {
      String[] ids = request.getParameterValues("ids");
      if (ids != null && !(ids.length == 0)) {
          expressShipPathService.delExpressShipPathByIDs(ids);
      }
      return mapping.findForward("main");
  }
  /**
    * 显示新增界面.
    * @param mapping struts mapping
    * @param form struts form
    * @param request http request
    * @param response http response
    * @return action forward
    * @throws Exception any execption
    */
   public ActionForward displayExpressShipPathCreateScreen(ActionMapping mapping,
           ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       // 产生并保存Token,防止重复提交
       saveToken(request);
       // 初始化输入框
       DynaValidatorForm expressShipPathForm = (DynaValidatorForm) mapping
               .getModuleConfig().findFormBeanConfig("expressShipPathForm")
               .createActionForm(servlet); 
        expressShipPathForm.set("id", ""); 
        expressShipPathForm.set("startPos", ""); 
        expressShipPathForm.set("endPos", ""); 
        expressShipPathForm.set("serviceType", ""); 
        expressShipPathForm.set("transPortType", ""); 
        expressShipPathForm.set("roadDistance", ""); 
        expressShipPathForm.set("estimateHours", ""); 
        expressShipPathForm.set("estimateCost", ""); 
        expressShipPathForm.set("quotedPrice", ""); 
        expressShipPathForm.set("urgentPrice", ""); 
        expressShipPathForm.set("noteDesc", "");
       // 将Struts form 设置到request中
       request.setAttribute("expressShipPathForm", expressShipPathForm);
       return mapping.findForward("ExpressShipPathCreate.screen");
   }
   /**
    * 显示更新操作.
    * @param mapping struts mapping
    * @param form struts form
    * @param request http request
    * @param response http response
    * @return action forward
    * @throws Exception any execption
    */
   public ActionForward displayExpressShipPathUpdateScreen(ActionMapping mapping,
           ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       String id = request.getParameter("id");
       ExpressShipPath expressShipPath = expressShipPathService.getExpressShipPathById(id);
       if (expressShipPath == null) {
	       ActionMessages errMsgs = new ActionMessages();
	       errMsgs.add("", new ActionMessage("errors.noSuchExpressShipPath"));
	       this.saveErrors(request, errMsgs);
	       return mapping.findForward("error.back");
       } 
       DynaValidatorForm expressShipPathForm = (DynaValidatorForm) mapping
               .getModuleConfig().findFormBeanConfig("expressShipPathForm")
               .createActionForm(servlet);
	       	   if(expressShipPath.getId()!=null){
	 	 	   expressShipPathForm.set("id",String.valueOf(expressShipPath.getId()));
	 	 	   }else{
	 	 	   expressShipPathForm.set("id",null);
	 	 	   }
	           expressShipPathForm.set("startPos",expressShipPath.getStartPos());
	           expressShipPathForm.set("endPos",expressShipPath.getEndPos());
	           expressShipPathForm.set("serviceType",expressShipPath.getServiceType());
	           expressShipPathForm.set("transPortType",expressShipPath.getTransPortType());
	       	   if(expressShipPath.getRoadDistance()!=null){
	 	 	   expressShipPathForm.set("roadDistance",String.valueOf(expressShipPath.getRoadDistance()));
	 	 	   }else{
	 	 	   expressShipPathForm.set("roadDistance",null);
	 	 	   }
	       	   if(expressShipPath.getEstimateHours()!=null){
	 	 	   expressShipPathForm.set("estimateHours",String.valueOf(expressShipPath.getEstimateHours()));
	 	 	   }else{
	 	 	   expressShipPathForm.set("estimateHours",null);
	 	 	   }
	       	   if(expressShipPath.getEstimateCost()!=null){
	 	 	   expressShipPathForm.set("estimateCost",String.valueOf(expressShipPath.getEstimateCost()));
	 	 	   }else{
	 	 	   expressShipPathForm.set("estimateCost",null);
	 	 	   }
	       	   if(expressShipPath.getQuotedPrice()!=null){
	 	 	   expressShipPathForm.set("quotedPrice",String.valueOf(expressShipPath.getQuotedPrice()));
	 	 	   }else{
	 	 	   expressShipPathForm.set("quotedPrice",null);
	 	 	   }
	       	   if(expressShipPath.getUrgentPrice()!=null){
	 	 	   expressShipPathForm.set("urgentPrice",String.valueOf(expressShipPath.getUrgentPrice()));
	 	 	   }else{
	 	 	   expressShipPathForm.set("urgentPrice",null);
	 	 	   }
	           expressShipPathForm.set("noteDesc",expressShipPath.getNoteDesc());
       // 将Struts form 设置到request中
       request.setAttribute("expressShipPathForm", expressShipPathForm);
       // 设置token 防止重复提交
       this.saveToken(request);
       return mapping.findForward("ExpressShipPathUpdate.screen");
   }
	/**
    * 显示预览.
    * @param mapping struts mapping
    * @param form struts form
    * @param request http request
    * @param response http response
    * @return action forward
    * @throws Exception any execption
    */
   public ActionForward displayExpressShipPathBrowseScreen(ActionMapping mapping,
           ActionForm form, HttpServletRequest request,
           HttpServletResponse response) throws Exception {
       String id = request.getParameter("id");
       ExpressShipPath expressShipPath = expressShipPathService.getExpressShipPathById(id);
       if (expressShipPath == null) {
	       ActionMessages errMsgs = new ActionMessages();
	       errMsgs.add("", new ActionMessage("errors.noSuchExpressShipPath"));
	       this.saveErrors(request, errMsgs);
	       return mapping.findForward("error.back");
       } 
   	   request.setAttribute("RQ_EXPRESSSHIPPATH",expressShipPath);
       return mapping.findForward("ExpressShipPathBrowse.screen");
   }
   /**
    * @param pExpressShipPathService the expressShipPathService to set
    */
   public void setExpressShipPathService(ExpressShipPathService pExpressShipPathService) {
       expressShipPathService = pExpressShipPathService;
   }
}
